import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:t3_data_service/biz_data_source/map/address/poi_search/model/t3_poi_enums.dart';
import 'package:t3_map/data_report/address_log.dart';
import 'package:t3_map/map_selected/widget/bottom_selected_poi_widget.dart';
import 'package:t3_map/map_selected/widget/map_selected_header_widget.dart';
import 'package:uikit/uikit.dart';
import 'dart:io';
import '../../configs/map_select_config.dart';
import 'map_selected_poi_page_controller.dart';

class MapSelectedPOIPage extends StatefulWidget {
  final MapSelectConfig config;
  const MapSelectedPOIPage({super.key, required this.config});

  @override
  State<MapSelectedPOIPage> createState() => _MapSelectedPOIPageState();
}

class _MapSelectedPOIPageState extends State<MapSelectedPOIPage> with PageVisibilityObserver {
  late MapSelectedPOIPageController controller;
  late T3PlatformView mapWidget = const T3PlatformView(viewType: 'map_selected_id');
  var androidDelay = false;
  
  final channel = const BasicMessageChannel(
      'MapSelectedPOIPage',
      StandardMessageCodec()
  );

  @override
  void onPageShow() {
    logMapSelectDebug('onPageShow');
    SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
        statusBarColor: Color(0x00EE0000),
        statusBarIconBrightness: Brightness.dark);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }

  @override
  void onPageHide() {
    logMapSelectDebug('onPageHide');
  }

  @override
  void onForeground() {
    logMapSelectDebug('onForeground');
  }

  @override
  void onBackground() {
    logMapSelectDebug('onBackground');
  }

  @override
  void initState() {
    super.initState();
    channel.setMessageHandler((message) {
      return controller.handlerMsg(message);
    });
    controller = MapSelectedPOIPageController(channel, widget.config);
    Future.delayed(const Duration(milliseconds: 100), () {
      if (mounted) {
        setState(() {
          androidDelay = true;
        });
      }
    });
    logMapSelectDebug('图选页面初始化:${widget.config.toJson().toString()}');
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    PageVisibilityBinding.instance.addObserver(this, ModalRoute.of(context)!);
  }

  @override
  void dispose() {
    logMapSelectDebug('图选页面销毁');
    PageVisibilityBinding.instance.removeObserver(this);
    channel.setMessageHandler(null);
    controller.dispose();

    SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset: false,
        extendBodyBehindAppBar: true,
        backgroundColor: LucyColors.lucy_F7F9FC,
        body: Stack(
          children: [
            if (androidDelay || Platform.isIOS) mapWidget,
            Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                if (widget.config.showSearchHeader)
                  ...[
                    Container(
                      height: MediaQuery.of(context).padding.top,
                      color: Colors.white,
                    ),
                    MapSelectedHeaderWidget(
                      type: widget.config.pointType,
                      cityScope: widget.config.cityScope,
                      viewModel: controller.headerViewModel,
                      config: widget.config,
                      clickBack: controller.clickBackButton,
                      clickCity: () {
                        controller.clickCity(context);
                      },
                      clickSearchBar: () {
                        controller.clickSearchBar();
                      },
                      clickCancel: controller.clickCancel,
                    ),
                ],
                if (!widget.config.showSearchHeader)
                  ...[
                    Row(
                      children: [
                        const SizedBox(width: 24),
                        Padding(
                          padding: EdgeInsets.only(top: (24 + MediaQuery.of(context).padding.top)),
                          child: GestureDetector(
                            onTap: controller.clickBackButton,
                            child: Container(
                              decoration: const BoxDecoration(
                                borderRadius: BorderRadius.all(Radius.circular(20)),
                                color: Colors.white,
                              ),
                              width: 40,
                              height: 40,
                              child: T3Image.t3assets(T3AssetsName.icon_back_elderly_slices),
                            ),
                          ),
                        ),
                        const Expanded(child: SizedBox()),
                      ],
                    ),
                ],
                const Expanded(child: SizedBox()),
                if (widget.config.pointType == T3POIType.start)
                  T3GestureDetector(
                    onTap: () {
                      controller.clickLocationButton();
                    },
                    child: Container(
                      width: 36,
                      height: 36,
                      margin: const EdgeInsets.only(bottom: 10, right: 10),
                      decoration: const BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(18))
                      ),
                      child: T3Image.t3assets(T3AssetsName.icon_taixflow_location)
                    ),
                  ),

                Obx(() {
                  if (controller.bottomSelectedPOIViewModel.hiddenBottomView.value) {
                    return const SizedBox();
                  } else {
                    return BottomSelectedPOIWidget(
                      viewModel: controller.bottomSelectedPOIViewModel,
                      config: widget.config,
                      clickIndexCallback: controller.clickPointsListAtIndex,
                      clickSureCallback: controller.clickSureButton,
                      clickCufusionIndexCallback: controller.clickConfusionAtIndex,
                    );
                  }
                }),
              ],
            )
          ],
        ));
  }
}
